//
//  LMCycleView.m
//  CycleScrollViewDemo
//
//  Created by lanou3g on 16/4/19.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMCycleView.h"

@interface LMCycleView ()<UIScrollViewDelegate>

///scrollView
@property (nonatomic, strong) UIScrollView *imgScrollView;
///pageController
@property (nonatomic, strong) UIPageControl *imgPageController;
///ImageViewArray(ImageView 的数组)
@property (nonatomic, strong) NSMutableArray *imgViewArray;
///存放图片的数组
@property (nonatomic, strong) NSMutableArray *imgArray;
///当前展示的图片下标
@property (nonatomic, assign) NSInteger index;
///定时器
@property (nonatomic, strong) NSTimer *imgTimer;
///点击回调block
@property (nonatomic, copy) void(^tapBlock)(NSInteger index);

///时间间隔
@property (nonatomic, assign) NSTimeInterval interval;

@end

@implementation LMCycleView


#pragma mark - 初始化

-(instancetype)initWithFrame:(CGRect)frame interVal:(NSTimeInterval)interval{
    self = [super initWithFrame:frame];
    if (self) {
        self.imgViewArray = [NSMutableArray array];
        self.imgArray         = [NSMutableArray array];
        self.interval = interval;
//        注意这里使用self.bounds
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
        [self addSubview:self.imgScrollView];
//        循环添加3个UIimageView
        for (int i = 0; i < 3; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
            ///添加到父视图，数组
            [self.imgScrollView addSubview:imgView];
            [self.imgViewArray addObject:imgView];
            
            ///按页滑动
            self.imgScrollView.pagingEnabled = YES;
            ///scrollView的水平指示线隐藏
            self.imgScrollView.showsHorizontalScrollIndicator = NO;
            //代理
            self.imgScrollView.delegate = self;
        }

    }
    return self;
}


#pragma mark - 图片名数组 setter

- (void)setImgNameArray:(NSMutableArray *)imgNameArray{
    if (_imgNameArray != imgNameArray) {
        _imgNameArray = nil;
        _imgNameArray = imgNameArray;
        ///在这里设置pageController
        [self.imgPageController removeFromSuperview];
        self.imgPageController = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40)];
        [self addSubview:self.imgPageController];
        ///属性
        self.imgPageController.currentPage = 0;
        self.imgPageController.numberOfPages = _imgNameArray.count;
//        设置pagecontroller的小点点不能用手势改变
        self.imgPageController.enabled = NO;
        
//        为imgArray添加照片
        for (int i = 0; i < _imgNameArray.count; i++) {
            UIImage *img = [UIImage imageNamed:imgNameArray[i]];
            [self.imgArray addObject:img];
            if (i < 3) {
                [self.imgViewArray[i] setImage:img];
            }
        }
//        设置当前偏移量
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.index = 0;
        [self layoutImages];
        

        
//        打开ImageView的手势交互
        [self.imgViewArray[1] setUserInteractionEnabled:YES];
//        点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCycleViewAction:)];
        [self.imgViewArray[1] addGestureRecognizer:tap];
    }
    
    //            初始化一个timer
    if (self.interval <= 0) {
        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }else{
        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
}


#pragma mark - 代理
//index的setter方法中实现了这个功能
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    self.imgPageController.currentPage = self.index;
//}

static CGFloat x = 0;
//开始拖动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    x = scrollView.contentOffset.x;
//    设置timer一个永远达不到的时间 ， 相当于停止定时器
    [self.imgTimer setFireDate:[NSDate distantFuture]];
}
//减速动画结束
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (x > scrollView.contentOffset.x) {
        [self panToLeft:NO Index:self.index];
    }else if (x < scrollView.contentOffset.x){
        [self panToLeft:YES Index:self.index];
    }
//     相当于开启定时器
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}


#pragma mark 向左滑动，向右滑动结束切换

- (void)panToLeft:(BOOL)left Index:(NSInteger)index{
    if (!left) {
        self.index = (index - 1 + self.imgNameArray.count) % self.imgNameArray.count;
    }else{
        self.index =( index + 1 ) % self.imgNameArray.count;
    }
//    切换图片
    [self layoutImages];
    
//    回位操作[视图不变，切换视图里面的照片]
    [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    
}

- (void)layoutImages{
    for (int i =0; i < 3; i++) {
        [self.imgViewArray[i] setImage:self.imgArray[(self.index - 1 + i + self.imgNameArray.count) % self.imgNameArray.count]];
    }
}


#pragma mark - index的setter方法
-(void)setIndex:(NSInteger)index{
    if (_index != index) {
        _index = index;
        self.imgPageController.currentPage = index;
    }
}



#pragma mark - 定时器图片滚动
- (void)timerAction{
    
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width *2, 0) animated:YES];
//    改变index
    self.index =(self.index + 1 ) % self.imgNameArray.count;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    /**
     *回位
     */
    [self.imgScrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0) animated:NO];
    [self layoutImages];
}

#pragma mark - 添加Block
- (void)addTapBlock:(void(^)(NSInteger index))block;{
    self.tapBlock = block;
}

///添加手势的实现
- (void)tapCycleViewAction:(UITapGestureRecognizer *)tap{
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}








@end
